package com.yanqu.road.server.cmd.activity.suitang.cross;

import com.yanqu.road.entity.activity.suitang.config.SuiTangConfigSync;
import com.yanqu.road.entity.activity.suitang.data.SuiTangUserData;
import com.yanqu.road.entity.activity.tongbing.config.TongBingConfig;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.TongBingProto;
import com.yanqu.road.pb.activity.suitang.SuiTang;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.suitang.SuiTangModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.suitang.SuiTangMgr;
import com.yanqu.road.server.manger.activity.tongbing.TongBingMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

@Cmd(code = GameProtocol.S_SUI_TANG_MOVE_SPEED_UP_FROM_CROSS, desc = "")
public class SuiTangMoveSpeedUpFromCrossCmd extends AbstractCommand {

    private SuiTang.SuiTangMoveSpeedUpRespMsg resp;
    private int realTimes;
    private int reqTimes;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        SuiTang.CrossSuiTangMoveSpeedUpRespMsg respMsg = SuiTang.CrossSuiTangMoveSpeedUpRespMsg.parseFrom(packet.getBytes());
        resp = respMsg.getResp();
        realTimes = respMsg.getRealTimes();
        reqTimes = respMsg.getReqTimes();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        SuiTangConfigSync config = SuiTangMgr.getConfig();

        // 预扣除返还
        SuiTangUserData userData = player.getModule(SuiTangModule.class).getUserData();
        if (userData == null) {
            ret = GameErrorCode.E_SUI_TANG_PARAM_ERR;
            return;
        }

        synchronized (userData) {
            int upNum = reqTimes;
            Property property = new Property(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(config.getParamConfig().getSuiTangCityMoveBuffCost(upNum)));
            player.getModule(PreReduceModule.class).restorePreReduce(property);

            // 成功了要扣消耗
            userData.setMoveSpeedUpNum(realTimes);  // 成功或者失败，都要以服务端的为主
            if (resp.getRet() == 0) {
                player.getModule(CurrencyModule.class).removeCurrency(property, eLogMoneyType.SuiTang, eLogMoneyType.SuiTangMoveSpeedUp);
            }
        }

        // 返回
        player.sendPacket(ClientProtocol.U_SUI_TANG_MOVE_SPEED_UP, resp.toBuilder());
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
